package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品是否已经添加到购物车当中
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        if (list != null && !list.isEmpty()) { //存在则把number + 1
            ShoppingCart card = list.get(0);
            log.info("{}的份数为:{}",card.getName(), card.getNumber());
            card.setNumber(card.getNumber() + 1);
            log.info("----------------------------------------------");
            log.info("{}的份数为:{}",card.getName(), card.getNumber());
            //更新数据
            shoppingCartMapper.update(card);
        } else { //不存在则添加到数据库当中
            if (shoppingCartDTO.getDishId() != null) { //添加的是菜品
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else { //添加的是套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.select(shoppingCart);
    }

    @Override
    public void deleteByUserId() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //先查询当前用户的购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart card = list.get(0);
            if(card.getNumber() == 1) {//判断要删除的东西是否只有一份, //一份就删除
                shoppingCartMapper.deleteById(card.getId());
            } else { //多份就减1
                card.setNumber(card.getNumber() - 1);
                shoppingCartMapper.update(card);
            }
        }
    }
}
